﻿using System;
using System.Threading;

namespace Wp7Tools.Util {
	public class Worker : IWorker {

		public static IWorker Default { get; set; }

		private Worker() {}

		public void RunAsync<T>(Func<T> method, Action<T> onSuccess, Action<Exception> onError) {
			ThreadPool.QueueUserWorkItem(o => {
				try {
					T resp = method.Invoke();
					onSuccess.Invoke(resp);
				}
				catch (Exception ex) {
					onError.Invoke(ex);
				}
			});
		}

		public void RunAsync(Action method, Action onSucess, Action<Exception> onError) {
			ThreadPool.QueueUserWorkItem(o => {
				try {
					method.Invoke();
					onSucess.Invoke();
				}
				catch (Exception ex) {
					onError.Invoke(ex);
				}
			});
		}
	}
}